package com.haha.project0301.dao;

import com.haha.project0301.pojo.Department;
import com.haha.project0301.pojo.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.*;

/**
 * @author dsx
 * @date 2021/3/1 16:42
 * @since JDK1.8
 */
@Component
public class EmpDao {
    @Autowired
    DepartmentDao departmentDao;
    
    private static Map<Integer, Employee> empMap = null;
    static{
        empMap = new HashMap<>();
        empMap.put(1,new Employee(1,"zhangsan","111@qq.com",1,new Department(1,"hr"),new Date()));
        empMap.put(2,new Employee(2,"lisi","222@qq.com",0,new Department(4,"经理"),new Date()));
        empMap.put(3,new Employee(3,"wangwu","333@qq.com",1,new Department(3,"苦逼的敲代码"),new Date()));
        empMap.put(4,new Employee(4,"zhaoliu","444@qq.com",0,new Department(2,"不会css的前端"),new Date()));
        empMap.put(5,new Employee(5,"tianqi","555@qq.com",0,new Department(5,"准备删库的架构"),new Date()));
    }
    private static int primaryId=6;
    
    public void addEmp(Employee emp){
        if (emp.getId()==null){
            emp.setId(primaryId++);
        }
        emp.setDepartment(departmentDao.getDepartmentById(emp.getDepartment().getDid()));
        empMap.put(emp.getId(),emp);//键是唯一的，所以可以通过改变value来实现 改 操作
    }
    
    public Collection<Employee> getAllEmp(){
        return empMap.values();
    }
    
    public Employee getEmpById(Integer id){
        return empMap.get(id);
    }

    public void deleteEmployeeByID(int id){
        empMap.remove(id);
    }
}
